package com.hollowlatte.manage.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hollowlatte.common.exceptionhandler.CustomException;
import com.hollowlatte.common.utils.Result;
import com.hollowlatte.manage.entity.Student;
import com.hollowlatte.manage.entity.vo.StudentVO;
import com.hollowlatte.manage.service.StudentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Hollow Latte
 * @since 2021-05-05
 */
@RequiresPermissions("user:student:*")
@Tag(name = "StudentController", description = "学生管理")
@CrossOrigin
@RestController
@RequestMapping("/student")
public class StudentController {
    private final StudentService studentService;

    public StudentController(StudentService studentService) {
        this.studentService = studentService;
    }


    @Operation(summary = "添加学生")
    @PostMapping()
    public Result addStudent(@RequestBody Student student) {
        boolean affect = studentService.save(student);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @Operation(summary = "通过id删除学生（逻辑删除）")
    @DeleteMapping()
    public Result deleteStudentById(@RequestParam Long studentId) {
        boolean affect = studentService.removeById(studentId);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }


    @Operation(summary = "根据id修改学生")
    @PutMapping()
    public Result updateStudentById(@RequestBody Student student) {
        boolean affect = studentService.updateById(student);

        if (affect) {
            return Result.ok();
        } else {
            return Result.error();
        }
    }

    @RequiresPermissions("user:student:check")
    @Operation(summary = "通过id查学生")
    @GetMapping("")
    public Result findById(@RequestParam Long studentId) {
        Subject subject = SecurityUtils.getSubject();
        if (subject.hasRole("student")) {
            Long loginStudentId = (Long) subject.getPrincipal();
            if (!loginStudentId.equals(studentId)) {
                throw new CustomException("A0400", "无权限查询其他学生信息");
            }
        }

        Student student = studentService.getById(studentId);
        if (ObjectUtils.isEmpty(student)) {
            throw new CustomException("A0400", "学生不存在");
        }

        StudentVO studentVO = new StudentVO();
        BeanUtils.copyProperties(student, studentVO);
        return Result.ok().data("student", studentVO);
    }


    @Operation(summary = "分页查询")
    @GetMapping("pagination")
    public Result paging(@RequestParam Long current, @RequestParam Long size) {
        Page<Student> studentPage = new Page<>(current, size);
        studentService.page(studentPage, null);

        Page<StudentVO> studentVOPage = new Page<>();
        BeanUtils.copyProperties(studentPage, studentVOPage);
        studentVOPage.setRecords(fillStudentToStudentVO(studentPage.getRecords()));

        return Result.ok().data("pageInfo", studentVOPage);
    }


    @Operation(summary = "多条件分页查询", description = "查询条件为name、gender")
    @PostMapping("pagination")
    public Result multiConditionPaging(@RequestParam Long current, @RequestParam Long size, @RequestBody(required = false) StudentVO studentVO) {
        Page<Student> studentPage = new Page<>(current, size);

        String name = studentVO.getName();
        String gender = studentVO.getGender();
        // 添加条件
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(name)) {
            studentQueryWrapper.like("name", name);
        }
        if (!StringUtils.isBlank(gender)) {
            studentQueryWrapper.eq("gender", gender);
        }

        studentService.page(studentPage, studentQueryWrapper);

        Page<StudentVO> studentVOPage = new Page<>();
        BeanUtils.copyProperties(studentPage, studentVOPage);

        studentVOPage.setRecords(fillStudentToStudentVO(studentPage.getRecords()));

        return Result.ok().data("pageInfo", studentVOPage);
    }


    private List<StudentVO> fillStudentToStudentVO(List<Student> studentList) {
        List<StudentVO> studentVOList = new ArrayList<>();

        for (Student student : studentList) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(student, studentVO);

            studentVOList.add(studentVO);
        }
        return studentVOList;
    }
}

